layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
      elem: '#startTime' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#endTime' //指定元素
    });

    /**
     * 运输管理统计管理
     */
    var ThirdManagementStatistics = {
        tableId: "thirdManagementStatisticsTable"
    };

    /**
     * 初始化表格的列
     */
    ThirdManagementStatistics.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'status', sort: true, title: '状态',
            	templet: function(d){
            		if (d.status == '已付'){
                    	return '<span style="color: #19be6b;font-weight: 600;">' + d.status + '</span>'
					} else {
                    	return '<span style="color: font-weight: 600;">' + d.status + '</span>'
					} 
            	}
        	},
            {field: 'v3', sort: true, title: '付款日期'},
            {field: 'v4', sort: true, title: '结算方式'},
            {field: 'zhouqi', sort: true, title: '结账周期'},
            {field: 'orderNumber', sort: true, title: '运单号'},
            {field: 'dates', sort: true, title: '日期'},
            {field: 'v1', sort: true, title: '货主'},
            {field: 'startings', sort: true, title: '起点'},
            {field: 'ends', sort: true, title: '终点', totalRowText: '合计：'},
            {field: 'freight', sort: true, title: '总运费¥', totalRow: true},
            {field: 'waiMoney', sort: true, title: '外调车运费¥', totalRow: true},
            {field: 'fuMoney', sort: true, title: '付运费¥', totalRow: true},
            {field: 'informationFee', sort: true, title: '信息费¥', totalRow: true},
            {field: 'profit', sort: true, title: '盈利¥', totalRow: true},
            {field: 'goodsName', sort: true, title: '货物名称'},
            {field: 'carNumber', sort: true, title: '车牌号'},
            {field: 'carMode', sort: true, title: '车型'},
            {field: 'v2', sort: true, title: '车主'},
            {field: 'driver', sort: true, title: '司机'},
            {field: 'tel', sort: true, title: '联系方式'},
            {field: 'remarks', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作',width:200,fixed: 'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ThirdManagementStatistics.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['status'] = $("#status").val();
        queryData['startings'] = $("#startings").val();
        queryData['ends'] = $("#ends").val();
        queryData['driver'] = $("#driver").val();
        queryData['tel'] = $("#tel").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['v1'] = $("#v1").val();
        queryData['v2'] = $("#v2").val();
        table.reload(ThirdManagementStatistics.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    ThirdManagementStatistics.openAddDlg = function () {
        layer.open({
            type: 2,
            title: '添加',
            area: ['1200px', '550px'],
            anim: 5,
            content: Feng.ctxPath + '/thirdManagementStatistics/add',
            end: function () {
                table.reload(ThirdManagementStatistics.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    ThirdManagementStatistics.exportExcel = function () {
        var checkRows = table.checkStatus(ThirdManagementStatistics.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ThirdManagementStatistics.openEditDlg = function (data) {
        layer.open({
            type: 2,
            title: '编辑',
            area: ['1200px', '550px'],
            anim: 5,
            content: Feng.ctxPath + '/thirdManagementStatistics/edit?id=' + data.id,
            end: function () {
                table.reload(ThirdManagementStatistics.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ThirdManagementStatistics.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/thirdManagementStatistics/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ThirdManagementStatistics.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ThirdManagementStatistics.tableId,
        url: Feng.ctxPath + '/thirdManagementStatistics/list',
        page: true,
        limit: 60,
		limits:[10,20,30,40,50,60,120,240,360,480],
        height: "full-60",
	    totalRow: true,
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: ThirdManagementStatistics.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ThirdManagementStatistics.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ThirdManagementStatistics.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ThirdManagementStatistics.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ThirdManagementStatistics.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ThirdManagementStatistics.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ThirdManagementStatistics.onDeleteItem(data);
        } else if (layEvent === 'print') {
	        layer.open({
	            type: 2,
	            title: '打印合同',
	            area: ['1000px', '550px'],
	            anim: 5,
	            content: Feng.ctxPath + '/thirdManagementStatistics/print?id='+data.id,
	            end: function () {
	            }
	        });
        }
    });
});
